<?php

namespace app\adminapiv3\service\module_manage\netfilter;

use app\adminapiv3\service\BaseService;
use app\common\definition\netfilter\NetfilterAccessLogDefinition;
use app\common\service\module_manage\netfilter\HostAccessLog;
use app\common\service\module_manage\netfilter\HostAccessLogConfig;
use app\common\service\module_manage\netfilter\HostLearnConfig;
use think\Db;

class AccessLogService extends BaseService
{
    /**
     * @name 列表
     * @param $hostId
     * @param $params
     * @return array
     * @author wx
     * @date 2019/10/31 15:41
     */
    public function list($hostId, $params)
    {
        $server = new HostAccessLog();
        $field = 'id,host_id, source_ip, target_ip';
        $list = $server->list($hostId, $params, $field, 'list');

        //数据处理
        foreach($list['data'] as $k => $v){
            $totalWhere['source_ip'] = $v['source_ip'];
            $totalWhere['target_ip'] = $v['target_ip'];
            $v['id'] = (string) $v['id'];
            $v['total'] = $server->gettotal($v['host_id'],$totalWhere);//总条数
            $v['total_out'] = $server->getTotalOut($v['host_id'],$totalWhere);//总出栈条数
            $v['total_input'] = $server->getTotalInput($v['host_id'],$totalWhere);//总入栈条数

            $list['data'][$k] = $v;
        }

        return $list;
    }

    /**
     * @name 详情
     * @param $hostId
     * @param $params
     * @return array
     * @author wx
     * @date 2019/10/31 16:57
     */
    public function detail($hostId, $params)
    {
        $server = new HostAccessLog();
        $field = 'id, source_ip, direction, target_ip, target_port, protocols, permission, first_time, last_time, view_all';
        $list = $server->list($hostId, $params, $field);

        //数据处理
        foreach($list['data'] as $k => $v){
            $v['id'] = (string) $v['id'];
            $v['first_time'] = date('Y-m-d H:i:s', $v['first_time']);
            $v['last_time'] = date('Y-m-d H:i:s', $v['last_time']);

            $list['data'][$k] = $v;
        }

        return $list;
    }

    /**
     * @name 访问日志报告开启
     * @author wx
     * @date time
     */
    public function logReportOn($hostId)
    {
        $HostLearnConfig = new HostLearnConfig();

        $HostLearnConfig->modifyLogStatus($hostId, NetfilterAccessLogDefinition::ENABLE);

        global $logArray;
        $host_name_s = $this->getHost('name', 'string');
        $logArray = array(
            'content' => '启用防火墙日志',
            'new_info' => '涉及主机:' . $host_name_s
        );

    }

    /**
     * @name 访问日志报告关闭
     * @author wx
     * @date time
     */
    public function logReportOff($hostId)
    {
        $HostLearnConfig = new HostLearnConfig();

        $HostLearnConfig->modifyLogStatus($hostId, NetfilterAccessLogDefinition::DISABLE);

        global $logArray;
        $host_name_s = $this->getHost('name', 'string');
        $logArray = array(
            'content' => '禁用防火墙日志',
            'new_info' => '涉及主机:' . $host_name_s
        );

    }

    /**
     * @name 读取日志选项
     * @param $hostId
     * @return array|null|\PDOStatement|string|\think\Model
     * @author wx
     * @date 2019/11/1 10:43
     */
    public function readConfig($hostId)
    {
        $logConfigDb = new HostAccessLogConfig();
        $configInfo = $logConfigDb->findData(['host_id' => $hostId]);

        return $configInfo;
    }

    /**
     * @name 修改日志选项
     * @param $hostId
     * @param $updateData
     * @return bool
     * @author wx
     * @date 2019/11/1 10:47
     */
    public function modifyConfig($input)
    {
        $logConfigDb = new HostAccessLogConfig();
        $condition[] = array('host_id', 'in', $input['host_id']);
        unset($input['host_id']);
        
        $logConfigDb->modify($condition, $input);
        $old_data = Db::name('host_access_log_config')->where($condition)->select();
        global $logArray;
        $host_name_a = $this->getHost('name');
        $old_info_a = array_map(function($v) use ($host_name_a){
            
            return '主机:' . $host_name_a[$v['host_id']] . '日志保留时间:'. $v['expired_time'] . '日志上传频率:' . $v['frequency'] . '秒' . '处理方式:' . $v['process'] == NetfilterAccessLogDefinition::EXPIRED_LOG_HANDLE_DELETE_NUMBER ? NetfilterAccessLogDefinition::EXPIRED_LOG_HANDLE_DELETE_NAME : NetfilterAccessLogDefinition::EXPIRED_LOG_HANDLE_KEEP_NAME;

        }, $old_data);

        $logArray = array(
            'content' => '修改防火墙日志选项',
            'new_info' => '配置修改为 日志保留时间:'. $input['expired_time'] . '日志上传频率:' . $input['frequency'] . '秒' . '处理方式:' . $input['process'] == NetfilterAccessLogDefinition::EXPIRED_LOG_HANDLE_DELETE_NUMBER ? NetfilterAccessLogDefinition::EXPIRED_LOG_HANDLE_DELETE_NAME : NetfilterAccessLogDefinition::EXPIRED_LOG_HANDLE_KEEP_NAME,
            'old_info' => implode(',', $old_info_a)
        );
    }


    /**
     * @name 忽略日志
     * @param $hostId
     * @param $id
     * @param $updateData
     * @author wx
     * @date 2019/11/1 13:21
     */
    public function ignoreLog($host_id, $id)
    {
        $server = new HostAccessLog();
        $condition[] = array(['id', 'in', $id]);
        $server->modify($host_id,$condition, ['ignore' => NetfilterAccessLogDefinition::LOG_IGNORE]);
        $host_s = $this->getHost('name', 'string');
        global $logArray;
        $logArray = array(
            'content' => '忽略访问日志',
            'new_info' => '主机:' . $host_s . '忽略日志:' . $id,
        );
    }

    /**
     * @name 取消忽略日志
     * @param $hostId
     * @param $id
     * @param $updateData
     * @author wx
     * @date 2019/11/1 13:21
     */
    public function unignoreLog($host_id, $id)
    {
        $server = new HostAccessLog();
        $condition[] = array(['id', 'in', $id]);
        $server->modify($host_id,$condition, ['ignore' => NetfilterAccessLogDefinition::LOG_CANCEL_IGNORE]);

        $host_s = $this->getHost('name', 'string');
        global $logArray;
        $logArray = array(
            'content' => '取消忽略访问日志',
            'new_info' => '主机:' . $host_s . '取消忽略日志ID为:' . $id,
        );
    }

    /**
     * @name: 告警列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-01-09 19:11:54
     */
    public function warringList ($filterData)
    {

        $HostAccessLog = new HostAccessLog();
        $return = $HostAccessLog->list($filterData['host_id'], $filterData, '*');

        return $return;
    }
}